<%@ page language="java" pageEncoding="UTF-8"%>
<script src="${baseURL}/js/handlebars/handlebars-min.js"></script>
<script src='${baseURL}/js/jquery.autocomplete.selector.js'></script>
<script src="${baseURL}/js/jquery.cookie.js" type="text/javascript"></script>
<script id="searchForm" type="text/x-handlebars-template">
<form class="navigator-search query-component generic-styled">
	<div class="search-criteria">
		<ul class="criteria-list">
			{{#if this}}
			<li class="criteria-actions">
				<button type="button"
					class="add-criteria aui-button aui-button-subtle drop-arrow"
					original-title="Add more fields to search with"
					style="display: inline-block;">更多</button>
			</li>
			{{/if}}
			<li>
				<button class="aui-button aui-button-subtle search-button"
					type="button" original-title="Search for issues">
					<span class="aui-icon aui-icon-small aui-iconfont-search bd bd-search"></span>
					搜索
				</button>
				<button class="aui-button aui-button-subtle reset-button"
					type="button" original-title="Search for issues">
					<span class="aui-icon aui-icon-small aui-iconfont-search bd bd-sync"></span>
					重置
				</button>
			</li>
		</ul>
	</div>
	{{#if this}}
	<div class="search-criteria-extended">
		<ul class="criteria-list">
		</ul>
	</div>
	{{/if}}
</form>
</script>
<script id="searchContainer" type="text/x-handlebars-template">
	<ul class="aui-list" data-id="{{id}}">
		{{#each param}}
		<li class="check-list-item imagebacked">
			<label class="item-label">
				<input type="checkbox" value="{{id}}" alt="{{text}}" class="inline-middle search-checkbox">
				<span class="inline-middle">
				{{text}}
				</span>
			</label>
		</li>
		{{/each}}
	</ul>
</script>
<script id="searchMultiContainer" type="text/x-handlebars-template">
	<ul class="aui-list" data-id="{{id}}">
		{{#each param}}
		<li class="check-list-item imagebacked">
			<label class="item-label">
				<input type="checkbox" value="{{id}}" alt="{{text}}" data-param={{params}} class="inline-middle search-multi-checkbox {{params}}">
				<span class="inline-middle">
				{{text}}
				</span>
			</label>
		</li>
		{{/each}}
	</ul>
</script>
<script id="searchAutoContainer" type="text/x-handlebars-template">
	<div class="check-list-field-container">
		<input autocomplete="off" placeholder="查找{{name}}..." class="aui-field check-list-field" 
			id="searcher-status-input" wrap="off">
		<span class="icon noloading"><i class="bd bd-search"></i></span>
	</div>
</script>
<script id="searchNumberContainer" type="text/x-handlebars-template">
	<div class="check-list-field-container" style="padding: 10px 10px;">
		<label for="withinTheLastRadio">在</label>
		<input name="minNum" placeholder="最小值" class="aui-field l-text"/>
		<label for="withinTheLastRadio">到</label>
		<input name="maxNum" placeholder="最大值" class="aui-field l-text"/>
		<label for="withinTheLastRadio">之间</label>
	</div>
	<div class="buttons-container form-footer" data-id="{{this}}">
		<input type="button" name="filter" class="aui-button filter-button" value="搜索">
	</div>
</script>
<script id="moreContainer" type="text/x-handlebars-template">
	<ul class="aui-list" id="searcher-status-suggestions">
		{{#each this}}
		<li class="check-list-item">
			<label class="item-label">
				<input type="checkbox" value="{{id}}" class="inline-middle selector-checkbox">
				<span class="inline-middle">
				{{name}}
				</span>
			</label>
		</li>
		{{/each}}
	</ul>
</script>
<script id="searchDateContainer" type="text/x-handlebars-template">
	<ul class="aui-list date-ul" data-id="{{id}}">
		{{#equals formate "yyyy-MM-dd"}}
		<li class="radio-list-item">
			<label class="item-label">
				<input type="radio" value="0" name="{{id}}" class="inline-middle selector-radio">
				<div class="inline-middle field-group">
					<label for="withinTheLastRadio">在</label>
					<input class="js-val-date" name="startTime" type="text">
					<label for="withinTheLastRadio">和</label>
					<input class="js-val-date" name="endTime" type="text">
					<label for="withinTheLastRadio">之间</label>
				</div>
			</label>
		</li>
		<li class="radio-list-item">
			<label class="item-label">
				<input type="radio" value="1" name="{{id}}" class="inline-middle selector-radio">
				<div class="inline-middle field-group">
					<label for="withinTheLastRadio">在过去的</label>
					<input class="js-val" name="afterTime" type="text">
					<select class="js-measurement afterTime-measurement" >
						<option value="m">分钟</option>
						<option value="h">小时</option>
						<option value="d">天</option>
						<option value="w">周</option>
						<option value="month">个月</option>
						<option value="y">年</option>
					</select>
					<label for="withinTheLastRadio">之内</label>
				</div>
			</label>
		</li>
		<li class="radio-list-item">
			<label class="item-label">
				<input type="radio" value="2" name="{{id}}" class="inline-middle selector-radio">
				<div class="inline-middle field-group">
					<label for="withinTheLastRadio">超过</label>
					<input class="js-val" name="beforeTime" type="text">
					<select class="js-measurement beforeTime-measurement">
						<option value="m">分钟</option>
						<option value="h">小时</option>
						<option value="d">天</option>
						<option value="w">周</option>
						<option value="month">个月</option>
						<option value="y">年</option>
					</select>
					<label for="withinTheLastRadio">之前</label>
				</div>
			</label>
		</li>
		{{/equals}}
		{{#equals formate "MM-dd"}}
		<div class="inline-middle field-group" style="padding: 10px 0px;">
			<label for="withinTheLastRadio">在</label>
			<input class="js-val-day" name="startDay" type="text">
			<label for="withinTheLastRadio">和</label>
			<input class="js-val-day" name="endDay" type="text">
			<label for="withinTheLastRadio">之间</label>
		</div>
		{{/equals}}
		{{#equals formate "yyyy-MM"}}
		<div class="inline-middle field-group" style="padding: 10px 0px;">
			<select name="year" class="field l-text">
				{{#each year}}
		    	<option value="{{id}}" {{#equals selected 1}}selected="selected"{{/equals}}>{{text}}</option>
				{{/each}}
		    </select>
			<select name="month" class="field l-text">
				{{#each month}}
		    	<option value="{{id}}" {{#equals selected 1}}selected="selected"{{/equals}}>{{text}}</option>
				{{/each}}
		    </select>
		</div>
		{{/equals}}
	</ul>
	<div class="buttons-container form-footer" data-id="{{id}}">
		<input type="button" name="filter" class="aui-button filter-button" value="搜索">
	</div>
</script>
<script id="selectorContainer" type="text/x-handlebars-template">
	{{#each this}}
	{{#equals selectType 0}}
	<li selector-id="{{id}}">
		<button type="button" data-id="{{id}}" data-value="{{value}}" class="criteria-selector aui-button aui-button-subtle drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 1}}
	<li class="text-query" selector-id="{{id}}">
		<div class="text-query-container inline-middle">
			<input class="search-entry text ajs-dirty-warning-exempt"
			id="{{id}}" type="text" name="{{id}}" placeholder="{{name}}" value="{{value}}">
		</div>
		{{#if isExtended}}
		<a href="#" class="remove-filter inline-middle remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 2}}
	<li class="check-query" selector-id="{{id}}">
		<div class="text-query-container inline-middle">
			<input id="{{id}}" type="checkbox" class="inline-middle search-check" name="{{id}}"
			no-check-value="{{noCheckValue}}"
			 value="{{checkValue}}" {{#equals value checkValue}}checked="checked"{{/equals}}> 
			<span class="selector-field-name inline-middle">{{name}}</span>
		</div>
		{{#if isExtended}}
		<a href="#" class="remove-filter inline-middle remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 3}}
	<li selector-id="{{id}}" class="autocomplete-query">
		<button type="button" data-id="{{id}}" data-tag="{{searchTag}}" data-value="{{value}}" class="criteria-selector criteria-selector-auto aui-button aui-button-subtle drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 4}}
	<li selector-id="{{id}}" class="user-defined-query">
		<button type="button" data-id="{{id}}" data-value="{{value}}" class="criteria-selector criteria-selector-defined aui-button aui-button-subtle drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 5}}
	<li selector-id="{{id}}" class="date-query">
		<button type="button" data-id="{{id}}" startTime="{{startTime}}" endTime="{{startTime}}" afterTime="{{afterTime}}" 
			beforeTime="{{beforeTime}}" data-type="{{type}}" data-measurement="{{measurement}}" class="criteria-selector criteria-selector-date aui-button aui-button-subtle 
			drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 6}}
	<li selector-id="{{id}}" class="number-query">
		<button type="button" data-id="{{id}}" class="criteria-selector criteria-selector-number aui-button aui-button-subtle drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 7}}
	<li class="autocomplete-input-query" selector-id="{{id}}">
		<div class="text-query-container inline-middle criteria-selector criteria-selector-input-auto" data-id="{{id}}" data-tag="{{searchTag}}" data-value="">
			<input class="search-auto-entry text ajs-dirty-warning-exempt"
			id="{{id}}" type="text" name="{{id}}" placeholder="{{name}}" value="{{value}}">
		</div>
		{{#if isExtended}}
		<a href="#" class="remove-filter inline-middle remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{#equals selectType 8}}
	<li selector-id="{{id}}" class="multi-selector">
		<button type="button" data-id="{{id}}" data-value="{{value}}" class="criteria-selector criteria-multi-selector aui-button aui-button-subtle drop-arrow {{#if isExtended}}criteria-selector-extended{{/if}}">
			<div class="criteria-wrap">
				<span class="fieldLabel">{{name}}：</span> 
				<div class="searcherValue">
					<span id="fieldfixfor" title="{{valueText}}" class="fieldValue">{{valueText}}</span>
				</div>
			</div>
		</button>
		{{#if isExtended}}
		<a href="#" class="select-remove-filter remove-filter-a" title="Remove criterion" tabindex="-1">
			<span class="aui-icon aui-icon-small aui-iconfont-remove bd bd-times-circle-o"></span>
		</a>
		{{/if}}
	</li>
	{{/equals}}
	{{/each}}
</script>
<script>
$("body").append('<div class="ajs-layer box-shadow"></div>');
$(function(){
	$(document).keypress(function(event){
		if($(".search-criteria").length>0){
			if (event.keyCode == 13) { // 13表示回车键
				search();
				return false;
			}
		}
	});
});

function initSelectorForm($element,isShow){
	if($element.find(".navigator-search").length==0){
		var template = Handlebars.compile($("#searchForm").html());
		var html = template(isShow);
		$element.prepend(html);
	}
}

//渲染选择器
function renderSelector(selector){
	var template = Handlebars.compile($("#selectorContainer").html());
	var extendSe = [];
	var se = []
	for(var i=0;i<selector.length;i++){
		if(selector[i].isExtended){
			extendSe.push(selector[i]);
		}else{
			se.push(selector[i])
		}
	}
	var html = template(se);
	$(".search-criteria>.criteria-list",$parentElement).prepend(html);
	html = template(extendSe);
	$(".search-criteria-extended>.criteria-list",$parentElement).prepend(html);
}

function renderNewSelector(selectorId, selectorValue, valueText){
	var selector = getSelectorById(selectorId);
	if(selectorValue!=null&&selectorValue!="" && selector != null){
		if(renderSelectorValue(selectorId,selectorValue,valueText)){
			renderSelector([selector]);
			addSelector(selector);
			renderSelectorValue(selectorId,selectorValue,valueText);
		}
	}else {
		renderSelectorValue(selectorId, selectorValue, valueText);
	}
}


function renderSelectorValue(selectorId,selectorValue,valueText){
	var flag = true;
	$(".criteria-list>li",$parentElement).each(function(){
		if($(this).attr("selector-id")==selectorId){
			if($(this).hasClass("text-query")){
				$(this).find("input").val(selectorValue);
			}else if($(this).hasClass("date-query")){
				if(selectorId!=null&&selectorId!=""){
					var selector = getSelectorById(selectorId);
					var selectorValueText = "";
					if(selector.formate=="yyyy-MM-dd"){
						if(selectorValue && selectorValue != ""){
							selectorValueText = updateTime($(this),selectorValue.dateType,selectorValue.startTime,selectorValue.endTime,
									selectorValue.measurement,selectorValue.afterTime,selectorValue.measurement,selectorValue.beforeTime);
						} else {
							selectorValueText = updateTime($(this),0,null,null);
						}
					}else if(selector.formate=="MM-dd"){
						selectorValueText = updateDay($(this),selectorValue.startDay,selectorValue.endDay);
					}else if(selector.formate=="yyyy-MM"){
						selectorValueText = updateMonth($(this),selectorValue.year,selectorValue.month);
					}
					$(this).find(".fieldValue").attr("title",selectorValueText||"全部");
					$(this).find(".fieldValue").text(selectorValueText||"全部");
				}
			}else if($(this).hasClass("number-query")){
				if(selectorValue){
					var selectorValueText = "";
					$(this).find(".criteria-selector").attr("minNum",selectorValue.minNum).attr("maxNum",selectorValue.maxNum);
					selectorValueText = updateNumber(selectorValue.minNum,selectorValue.maxNum);
					$(this).find(".fieldValue").attr("title",selectorValueText||"全部");
					$(this).find(".fieldValue").text(selectorValueText||"全部");
				}else{
					var selectorValueText = "";
					$(this).find(".criteria-selector").attr("minNum",null).attr("maxNum",null);
					selectorValueText = updateNumber(null,null);
					$(this).find(".fieldValue").attr("title",selectorValueText||"全部");
					$(this).find(".fieldValue").text(selectorValueText||"全部");
				}
			}else if ($(this).hasClass("multi-selector")){
				var selectorParam = getSearchContainerParam(selectorId);
				var selectorValueText = "";
				if(selectorValue != null || selectorValue != '') {
					var data_value = '';
					selectorValueText = '';
					$.each(selectorValue, function(i, e) {
						var tmp = '';
						if(i != 0) {
							tmp = ';';
						}
						tmp += '{"param":"' + e.param + '","value":"' + e.value + '"}';
						data_value += tmp;
						$.each(selectorParam.param, function(i, e2){
							if(e2.params == e.param && e2.id == e.value) {
								if(selectorValueText != '') {
									selectorValueText += "，"+ e2.text;
								}else{
									selectorValueText += e2.text;
								}
								return false;
							}
						});
					});
				}
				if(selectorValueText!=""){
					$(this).find(".criteria-selector").attr("data-value",data_value);
					$(this).find(".fieldValue").attr("title",selectorValueText);
					$(this).find(".fieldValue").text(selectorValueText);
				}else{
					$(this).find(".criteria-selector").attr("data-value","");
					$(this).find(".fieldValue").attr("title","全部");
					$(this).find(".fieldValue").text("全部");
				}
				
			}else{
				if(selectorId!=null&&selectorId!=""){
					var selectorValueText = "";
					var selectorParam = getSearchContainerParam(selectorId);
					var valueList = selectorValue.split(";");
					if(selectorParam!=null&&selectorParam!=""&&selectorParam!="undefined"){
						// valueList[0]如果是空串进行的是清空动作，不设置selectorValueText
						if(valueList[0] != ""){
							for(var j=0;j<valueList.length;j++){
								if(valueList[j]!=null&&valueList[j]!=''){
									for(var i=0;i<selectorParam.param.length;i++){
										if((selectorParam.param)[i].id == valueList[j]){
											if(selectorValueText!=""){
												selectorValueText += "，"+(selectorParam.param)[i].text;
											}else{
												selectorValueText += (selectorParam.param)[i].text;
											}
										}
									}
								}
							}
						}
						
						if(selectorValueText!=""){
							$(this).find(".criteria-selector").attr("data-value",selectorValue);
							$(this).find(".fieldValue").attr("title",selectorValueText);
							$(this).find(".fieldValue").text(selectorValueText);
							$(this).find(".search-auto-entry").attr("data-value",selectorValueText);
						}else{
							$(this).find(".criteria-selector").attr("data-value","");
							$(this).find(".fieldValue").attr("title","全部");
							$(this).find(".fieldValue").text("全部");
						}
					}
				}
				if(valueText!="undefined"&&valueText!=null&&valueText!=""){
					$(this).find(".criteria-selector").attr("tag-value",valueText);
					$(this).find(".fieldValue").attr("title",valueText);
					$(this).find(".fieldValue").text(valueText);
				}
			}
			flag = false;
			return false;
		}
	});
	return flag;
}

//删除选择器
function removeSelector(selectorId){
	$(".criteria-list>li",$parentElement).each(function(){
		if($(this).attr("selector-id")==selectorId){
			$(this).remove();
			search();
			return false;
		}
	});
}

//点击选择器
$(document).on("click",".criteria-selector",function(){
	if($(this).hasClass("active")){
		$("body").click();//隐藏本身选择器选项
	}else{
		$("body").click();//隐藏其他选择器选项
		var dataId = $(this).attr("data-id");
		var dataValue = $(this).attr("data-value");
		var searchParam = getSearchContainerParam(dataId);
		$(".ajs-layer").children().remove();
		if($(this).hasClass("criteria-selector-auto")){
			var template = Handlebars.compile($("#searchAutoContainer").html());
			var param = [];
			for(var i=0;i<searchParam.param.length&&i<6;i++){
				param.push(searchParam.param[i]);
			}
			var showParam = {id:searchParam.id,name:searchParam.name,param:param}
			var html = template(showParam);
			$(".ajs-layer").append(html);
			initAuto(dataId,searchParam.param);
			var value = dataValue.split(";");
			for(var i=0;i<value.length;i++){
				$(".ajs-layer").find("input[value='"+value[i]+"']").attr("checked","checked");
			}
		}else if($(this).hasClass("criteria-selector-input-auto")){
			searchInitAuto(dataId,searchParam.param,$("#"+dataId,$parentElement));
			var value = dataValue.split(";");
			for(var i=0;i<value.length;i++){
				$(".ajs-layer").find("input[value='"+value[i]+"']").attr("checked","checked");
			}
		}else if($(this).hasClass("criteria-selector-defined")){
			var selector = getSelectorById(dataId);
			if(selector&&selector.render){
				var $element = $('<ul class="select-defined"></ul>').appendTo($(".ajs-layer"));
				selector.render($element,searchParam);
			}
		}else if($(this).hasClass("criteria-selector-number")){
			var template = Handlebars.compile($("#searchNumberContainer").html());
			var html = template(dataId);
			$(".ajs-layer").append(html);
			$(".ajs-layer").find("input[name='minNum']").val($(this).attr("minNum"));
			$(".ajs-layer").find("input[name='maxNum']").val($(this).attr("maxNum"));
		}else if($(this).hasClass("criteria-selector-date")){
			var selector = getSelectorById(dataId);
			var template = Handlebars.compile($("#searchDateContainer").html());
			if(selector.formate=="yyyy-MM-dd"){
				var html = template({id:dataId,formate:selector.formate});
				$(".ajs-layer").append(html);
				var type = $(this).attr("data-type");
				$(".ajs-layer").find(".selector-radio[value='"+type+"']").attr("checked","checked");
				if(type==0){
					$(".ajs-layer").find("input[name='startTime']").val($(this).attr("startTime"));
					$(".ajs-layer").find("input[name='endTime']").val($(this).attr("endTime"));
				}else if(type==1){
					$(".ajs-layer .afterTime-measurement").children("option[value='"+$(this).attr("data-measurement")+"']").attr("selected","selected");
					$(".ajs-layer").find("input[name='afterTime']").val($(this).attr("afterTime"));
				}else if(type==2){
					$(".ajs-layer .beforeTime-measurement").children("option[value='"+$(this).attr("data-measurement")+"']").attr("selected","selected");
					$(".ajs-layer").find("input[name='beforeTime']").val($(this).attr("beforeTime"));
				}
				/* $(".ajs-layer").find(".js-val-date").ligerDateEditor(); */
				$(".ajs-layer").find(".js-val-date").datetimepicker({
				    language:  'zh-CN',  
					autoclose: 1,
					minView: 2,
					format: 'yyyy-mm-dd'
				});
			}else if(selector.formate=="MM-dd"){
				var html = template({id:dataId,formate:selector.formate});
				$(".ajs-layer").append(html);
				$(".ajs-layer").find("input[name='startDay']").val($(this).attr("startDay"));
				$(".ajs-layer").find("input[name='endDay']").val($(this).attr("endDay"));
				//$(".ajs-layer").find(".js-val-day").ligerDateEditor({ format: "MM-dd"});
				$(".ajs-layer").find(".js-val-day").datetimepicker({
				    language:  'zh-CN',  
					autoclose: 1,
					minView: 2,
					format: 'yyyy-mm-dd'
				});
			}else if(selector.formate=="yyyy-MM"){
				var year = getYear($(this).attr("year"));
				var month = getMonth($(this).attr("month"));
				var html = template({id:dataId,formate:selector.formate,year:year,month:month});
				$(".ajs-layer").append(html);
			}
		}else if($(this).hasClass("criteria-multi-selector")){
			var template = Handlebars.compile($("#searchMultiContainer").html());
			var html = template(searchParam);
			$(".ajs-layer").append(html);
			var value = dataValue.split(";");
			for(var i=0;i<value.length;i++){
				if(value[i]!=null&&value[i]!=""&&value[i]!="undefined"){
					var valueJson =  eval('(' + value[i] + ')');
					$(".ajs-layer").find(".search-multi-checkbox."+valueJson.param+"[value='"+valueJson.value+"']").attr("checked","checked");
				}
			}
		}else{
			var template = Handlebars.compile($("#searchContainer").html());
			var html = template(searchParam);
			$(".ajs-layer").append(html);
			var value = dataValue.split(";");
			for(var i=0;i<value.length;i++){
				if(value[i]!=""){
					$(".ajs-layer").find("input[value='"+value[i]+"']").attr("checked","checked");
				}
			}
		}
		$(this).addClass("active");
		setLayerPosition($(this));
	}
	return false;
});

//设置下拉框的位置
function setLayerPosition(obj){
	var left = parseFloat($(obj).offset().left);
	var width = parseFloat($("body").width());
	var layerWidth = parseFloat($(".ajs-layer").width());
	if(width-left<layerWidth){
		var right = width - (left+parseFloat($(obj).parent().width()));
		$(".ajs-layer").attr("style","right:"+right+"px;top:"+(parseFloat($(obj).offset().top)+parseFloat($(obj).parent().height())-1)+"px;").addClass("active");
	}else{
		$(".ajs-layer").attr("style","left:"+left+"px;top:"+(parseFloat($(obj).offset().top)+parseFloat($(obj).parent().height())-1)+"px;").addClass("active");
	}
}

//点击更多
$(document).on("click",".add-criteria",function(){
	if($(this).hasClass("active")){
		$("body").click();//隐藏本身选择器选项
	}else{
		$("body").click();//隐藏其他选择器选项
		var template = Handlebars.compile($("#moreContainer").html());
		var html = template(getMoreSelector());
		$(".ajs-layer").children().remove();
		$(".ajs-layer").append(html);
		var selector = getCurrentSelector();
		for(var i=0;i<selector.length;i++){
			if(selector[i].isExtended){
				$(".ajs-layer").find("input[value='"+selector[i].id+"']").attr("checked","checked");
			}
		}
		$(this).addClass("active");
		setLayerPosition($(this));
	}
	return false;
});

//点击更多的选择项
$(document).on("change","input.selector-checkbox",function(){
	if($(this).is(':checked')){
		var selector = getSelectorById($(this).val());
		if(selector!=null&&selector!=""&&selector!="undefined"){
			renderSelector([selector]);
			addSelector(selector);
			if(selector.selectType==2){
				search();
			}
		}
	}else{
		removeSelector($(this).val());
		deleteSelector($(this).val());
		search();
	}
	setLayerPosition($(".add-criteria",$parentElement));
	return false;
});

//点击选择器的选择项
$(document).on("change","input.search-checkbox",function(){
	var value = "";
	var valueText = "";
	$(".ajs-layer").find("input.search-checkbox:checked").each(function(){
		if(valueText!=""){
			value += ";"+$(this).val();
			valueText += "，"+$(this).attr("alt");
		}else{
			value = $(this).val();
			valueText = $(this).attr("alt");
		}
	});
	var id = $(".ajs-layer").find("ul").attr("data-id");
	
	$(".criteria-list>li",$parentElement).each(function(){
		if($(this).attr("selector-id")==id){
			if(value==null||value==""){
				valueText = "全部";
			}
			$(this).find(".criteria-selector").attr("data-value",value);
			$(this).find(".fieldValue").attr("title",valueText);
			$(this).find(".fieldValue").text(valueText);
			return false;
		}
	});
	search();
	return false;
});

//点击自动匹配选择器的选择项
$(document).on("change","input.search-auto-checkbox",function(){
	var value = "";
	var valueText = "";
	$(".ajs-layer").find("input.search-auto-checkbox:checked").each(function(){
		if(value!=""&&value.indexOf($(this).val())==-1){
			value += ";"+$(this).val();
			valueText += "，"+$(this).attr("alt");
		}else{
			value = $(this).val();
			valueText = $(this).attr("alt");
		}
	});
	var id = $(".ajs-layer").find(".ac-ul").attr("data-id");
	
	$(".criteria-list>li",$parentElement).each(function(){
		if($(this).attr("selector-id")==id){
			$(this).find(".criteria-selector").attr("data-value",value);
			$(this).find(".ac_input").attr("data-value",valueText);
			$(this).find(".fieldValue").attr("title",valueText);
			$(this).find(".fieldValue").text(valueText);
			return false;
		}
	});
	search();
	return false;
});

//点击多类型的选择器的选择项
$(document).on("change","input.search-multi-checkbox",function(){
	var value = "";
	var valueText = "";
	$(".ajs-layer").find("input.search-multi-checkbox:checked").each(function(){
		var dd = {param:$(this).attr("data-param"),value:$(this).val()};
		if(valueText!=""){
			value += ";"+JSON.stringify(dd);
			valueText += "，"+$(this).attr("alt");
		}else{
			value = JSON.stringify(dd);
			valueText = $(this).attr("alt");
		}
	});
	var id = $(".ajs-layer").find("ul").attr("data-id");
	
	$(".criteria-list>li",$parentElement).each(function(){
		if($(this).attr("selector-id")==id){
			if(value==null||value==""){
				valueText = "全部";
			}
			$(this).find(".criteria-selector").attr("data-value",value);
			$(this).find(".fieldValue").attr("title",valueText);
			$(this).find(".fieldValue").text(valueText);
			return false;
		}
	});
	search();
	return false;
});

//点击搜索
$(document).on("click",".filter-button",function(){
	var id = $(this).parent().attr("data-id");
	var selector = getSelectorById(id);
	var valueText = "";
	if(selector){
		if(selector.selectType==5){
			$(".criteria-list>li",$parentElement).each(function(){
				if($(this).attr("selector-id")==id){
					if(selector.formate=="yyyy-MM-dd"){
						valueText = updateTime($(this),$(".selector-radio:checked").val(),$("input[name='startTime']").val(),$("input[name='endTime']").val(),
								$(".afterTime-measurement").val(),$("input[name='afterTime']").val(),
								$(".beforeTime-measurement").val(),$("input[name='beforeTime']").val());
					}else if(selector.formate=="MM-dd"){
						valueText = updateDay($(this),$("input[name='startDay']").val(),$("input[name='endDay']").val());
					}else if(selector.formate=="yyyy-MM"){
						valueText = updateMonth($(this),$("select[name='year']").val(),$("select[name='month']").val());
					}
					$(this).find(".fieldValue").attr("title",valueText);
					$(this).find(".fieldValue").text(valueText);
					$("body").click();
					search();
					return false;
				}
			});
		}else if(selector.selectType==6){
			$(".criteria-list>li",$parentElement).each(function(){
				if($(this).attr("selector-id")==id){
					$(this).find(".criteria-selector").attr("minNum",$("input[name='minNum']").val()).attr("maxNum",$("input[name='maxNum']").val());
					valueText = updateNumber($("input[name='minNum']").val(),$("input[name='maxNum']").val())
					$(this).find(".fieldValue").attr("title",valueText);
					$(this).find(".fieldValue").text(valueText);
					$("body").click();
					search();
					return false;
				}
			});
		}
	}
	return false;
});

var measurementData = {m:"分钟",h:"小时",d:"天",w:"周",month:"个月",y:"年"};

//更新时间选择器的值
function updateTime(obj,type,startTime,endTime,afterMeasurement,afterTime,beforeMeasurement,beforeTime){
	var $selector = $(obj).find(".criteria-selector");
	$selector.attr("data-type",type);
	var valueText = "全部";
	if(type==0){
		$selector.attr("startTime",startTime).attr("endTime",endTime)
			.attr("data-measurement",null).attr("afterTime",null).attr("beforeTime",null);
		if((startTime!=null&&startTime!="") && (endTime!=null&&endTime!=null)){
			valueText = "在"+startTime+"和"+endTime+"之间";
		}else if(startTime!=null&&startTime!=""){
			valueText = "从"+startTime+"开始";
		}else if(endTime!=null&&endTime!=""){
			valueText = "在"+endTime+"之前";
		}
	}else if(type==1){
		$selector.attr("startTime",null).attr("endTime",null)
			.attr("data-measurement",afterMeasurement).attr("afterTime",afterTime).attr("beforeTime",null);
		if(afterTime!=null&&afterTime!=""){
			valueText = "在过去的"+afterTime+measurementData[afterMeasurement]+"之内";
		}
	}else if(type==2){
		$selector.attr("startTime",null).attr("endTime",null)
			.attr("data-measurement",beforeMeasurement).attr("afterTime",null).attr("beforeTime",beforeTime);
		if(beforeTime!=null&&beforeTime!=""){
			valueText = "超过"+beforeTime+measurementData[beforeMeasurement]+"之前";
		}
	}
	return valueText;
}

//更新日期选择器的值
function updateDay(obj,startDay,endDay){
	if(startDay == undefined || startDay == "")
		startDay = null ;
	if(endDay == undefined || endDay == "")
		endDay = null ;
	var $selector = $(obj).find(".criteria-selector");
	$selector.attr("startDay",startDay).attr("endDay",endDay);
	if((startDay!=null&&startDay!="") && (endDay!=null&&endDay!="")){
		return "在"+startDay+"和"+endDay+"之间";
	}else if(startDay!=null&&startDay!=""){
		return "从"+startDay+"开始";
	}else if(endDay!=null&&endDay!=null){
		return "在"+endDay+"之前";
	}else{
		return "全部";
	}
}

//更新月份选择器的值
function updateMonth(obj,year,month){
	var $selector = $(obj).find(".criteria-selector");
	$selector.attr("year",year).attr("month",month);
	if((year!=null&&year!="") && (month!=null&&month!=null)){
		return year+"年"+month+"月";
	}else{
		return "全部";
	}
}

function updateNumber(min,max){
	if((min!=null&&min!="") && (max!=null&&max!="")){
		return "在"+min+"到"+max+"之间";
	}else if(min!=null&&min!=""){
		return "大于等于"+min;
	}else if(max!=null&&max!=""){
		return "小于等于"+max;
	}else{
		return "全部";
	}
}

//输入搜索的文本框
$(document).on("change","input.search-entry",function(){
	search();
	return false;
});

/* //输入自动匹配搜索的文本框
$(document).on("change","input.search-auto-entry",function(){
	search();
	return false;
}); */

//点击checkbox的选择器
$(document).on("change","input.search-check",function(){
	search();
	return false;
});

//点击X，删除选择器
$(document).on("click",".remove-filter-a",function(){
	var id = $(this).parent().attr("selector-id");
	$("body").click();
	removeSelector(id);
	deleteSelector(id);
	search();
	return false;
});

//点击checkbox选择器的label
$(document).on("click",".check-query",function(e){
	if(e.target.nodeName!="input"&&e.target.nodeName!="INPUT"){
		if($(this).find("input:checkbox").is(':checked')){
			$(this).find("input:checkbox").removeAttr("checked");
		}else{
			$(this).find("input:checkbox").prop("checked","checked");
		}
		search();
	}
});

$(document).on("click",".search-button",function(e){
	search();
	return false;
});

$(document).on("click",".reset-button",function(e){
	resetSelector();
	search();
	searchGrid.bootstrapTable('selectPage',1);//重置跳回第一页
	return false;
});

//点击除下拉选择以外的地方，收起下拉选择
$(document).click(function(e){
	var flag = true;
	$(e.target).parents().each(function(){
		if($(this).hasClass("ajs-layer")||$(this).hasClass("l-box-dateeditor")){
			flag = false;
			return;
		}
	});
	if(flag){
		$(".ajs-layer").removeClass("active");
		$(".criteria-selector").removeClass("active");
		$(".add-criteria").removeClass("active");
	}
});

//获取选择器的搜索值
function getSelectorValue(){
	var data = {};
	$(".criteria-list>li",$parentElement).each(function(){
		var id = $(this).attr("selector-id");
		if(id!=null&&id!=""&&id!="undeifned"){
			var value = null;
			if($(this).hasClass("text-query")){
				value = $(this).find("input").val();
			}else if($(this).hasClass("check-query")){
				if($(this).find("input").is(":checked")){
					value = $(this).find("input").val();
				}else{
					value = $(this).find("input").attr("no-check-value");
				}
			}else if($(this).hasClass("autocomplete-query")){
				value = $(this).children(".criteria-selector").attr("data-value");
				var tagValue = $(this).children(".criteria-selector").attr("tag-value");
				if((value==null||value=="") && (tagValue!=null&&tagValue!="")){
					data[$(this).children(".criteria-selector").attr("data-tag")] = tagValue;
				}
			}else if($(this).hasClass("autocomplete-input-query")){
				value = $(this).children(".criteria-selector").attr("data-value");
				var tagValue = $(this).find(".search-auto-entry ").val();
				if((value==null||value=="") && (tagValue!=null&&tagValue!="")){
					data[$(this).children(".criteria-selector").attr("data-tag")] = tagValue;
				}
			}else if($(this).hasClass("date-query")){
				var obj = $(this).children(".criteria-selector");
				var selector = getSelectorById(id);
				value = {};
				if(selector.formate=="yyyy-MM-dd"){
					value.dateType = $(obj).attr("data-type");
					value.startTime = $(obj).attr("startTime");
					value.endTime = $(obj).attr("endTime");
					value.afterTime = $(obj).attr("afterTime");
					value.beforeTime = $(obj).attr("beforeTime");
					value.measurement = $(obj).attr("data-measurement");
				}else if(selector.formate=="MM-dd"){
					value.startDay = $(obj).attr("startDay");
					value.endDay = $(obj).attr("endDay");
				}else if(selector.formate=="yyyy-MM"){
					value.year = $(obj).attr("year");
					value.month = $(obj).attr("month");
				}
				value = JSON.stringify(value);
			}else if($(this).hasClass("number-query")){
				value = {};
				var obj = $(this).children(".criteria-selector");
				value.minNum = $(obj).attr("minNum");
				value.maxNum = $(obj).attr("maxNum");
				value = JSON.stringify(value);
			}else if($(this).hasClass("multi-selector")){
				var selector = getSelectorById(id);
				var dataValue = $(this).children(".criteria-selector").attr("data-value");
				var values = dataValue.split(";");
				for(var i=0;i<values.length;i++){
					if(values[i]!=""){
						var dd = JSON.parse(values[i]);
						if(data.hasOwnProperty(dd.param)&&selector.trueOrFalse){
							delete data[dd.param];
						}else{
							data[dd.param] = dd.value;
						}
					}
				}
				dataValue = dataValue.replace(/;/g, ',');
				data[id] = '[' + dataValue + ']';
			}else{
				value = $(this).children(".criteria-selector").attr("data-value");
			}
			if(value!=null&&value!=""&&value!="undefined"&&value!="{}"){
				data[id] = value;
			}
		}
	});
	return data;
}

//初始化自动匹配
function initAuto(selectorId,data){
	$("#searcher-status-input").autocompleteSelector(data,{
		minChars:0,
		max:6,
		mustMatch:false,
		scrollHeight: 250,
		matchContains: true,
		selectFirst:false,
		scroll:false,
		cacheLength:30,
		selectorId: selectorId,
		formatValue: function(item) { 
			return item.id;
		},
		formatItem: function(item) { 
			return item.text;
		},
		formatResult:function(item) { 
			return item.text;
		},
		enterEvent:function(data){
			renderSelectorValue(selectorId,null,data);
			search();
		}
	}).result(function(event,item) {
		if(item.id!=null||item.id!=""){
			//renderSelectorValue(selectorId,item.id);
		}
	});
}

function searchInitAuto(selectorId,data,$element){
	if(!$element.hasClass(".ac_input")){
		$element.autocompleteSelector(data,{
			minChars:0,
			max:6,
			mustMatch:false,
			scrollHeight: 250,
			matchContains: true,
			selectFirst:false,
			scroll:false,
			multiple: true,
			multipleSeparator: "，",
			cacheLength:30,
			selectorId: selectorId,
			formatValue: function(item) { 
				return item.id;
			},
			formatItem: function(item) { 
				return item.text;
			},
			formatResult:function(item) { 
				return item.text;
			},
			enterEvent:function(data){
				renderSelectorValue(selectorId,null,data);
				search();
			}
		}).result(function(event,item) {
			if(item.id!=null||item.id!=""){
				//renderSelectorValue(selectorId,item.id);
			}
		});
	}
}

function search(init,json){
	var data = getSelectorValue();
	if(json){
		for(var key in json){
			data[key] = json[key];
		}
	}
	
/*	searchGrid.pageNumber=1;
 	if(init){
		searchGrid.set('parms', data);
		searchGrid.set("dataAction",'server');
		searchGrid._setUrl(searchUrl);
	}else{
		searchGrid.set('parms', data);
		searchGrid.loadData();
	} */
	
	//初始化加载第一页
	searchGrid.bootstrapTable({
		pageNumber:1
	});
	if(init){
		searchGrid.bootstrapTable('refresh',{query:data,url:searchUrl});		
	}else{
		searchGrid.bootstrapTable('refresh',{query:data});
	} 

}

//默认显示的选择器，不允许删除的
var defaultSelector = [];
//可扩展的选择器，可以删除和添加
var extendSelector = [];
//列表
var searchGrid = null;
//搜索地址
var searchUrl = null;
//搜索Tab bootstrap不存在tab对象
var searchTab = null;
//父类元素
var $parentElement = $("body");

function initData($element,defaultSelector,extendSelector){
	$parentElement = $element;
	if(extendSelector.length>0){
		initSelectorForm($element,true);
	}else{
		initSelectorForm($element,false);
	}
	setDefaultSelector(defaultSelector);
	setExtendSelector(extendSelector);
	
}

function initSearchGrid(cgrid,curl,json){
	searchGrid = cgrid;
	searchUrl = curl;
	if(curl){
		search(true,json);
	}
}

function setDefaultSelector(selector){
	defaultSelector = selector;
}

function getDefaultSelector(){
	return defaultSelector;
}

function setExtendSelector(selector){
	extendSelector = selector;
}

function getExtendSelector(){
	return extendSelector;
}

//选择器的选项内容
var searchContainer = {};

//设置选择器的选项内容
function setSearchContainer(selectorId,name,searchParam){
	searchContainer[selectorId] = {id:selectorId,name:name,param:searchParam};
}
 
//获取所有选择器的选项内容
function getSearchContainer(){
	return searchContainer;
}
 
//获取某个选择器的选项内容
function getSearchContainerParam(selectorId){
	return searchContainer[selectorId];
}

//获取缓存的选择器
function getSelector(){
	try{
		var selector = $.cookie('selectorContainer'+searchTab);
		if(selector==null || selector=="" || selector=="undefined"){
			selector = null;
		}else{
			selector = JSON.parse(selector);
		}
		return selector;
	}catch(e){
		setSelector(null);
	}
}

//获取当前要显示的选择器，如果缓存内有，则显示缓存的，没有则显示
function getCurrentSelector(){
	var selectors = getSelector();
	var selector = [];
	if(selectors==null){
		selector = defaultSelector;
	}else{
		for(var value in selectors){
			var s = getSelectorById(selectors[value]);
			if(s){
				selector.push(s);
			}
		}
		for(var i=0;i<defaultSelector.length;i++){
			selector.push(defaultSelector[i]);
		}
	}
	return selector;
}

//设置选择器的数据到缓存中
function setSelector(selector){
	$.cookie('selectorContainer'+searchTab, JSON.stringify(selector), { expires: 7 });
}

//添加选择器到缓存中
function addSelector(selector){
	var cselector = getSelector();
	if(cselector==null){
		cselector = [];
	}
	cselector.push(selector.id);
	setSelector(cselector);
}

//从缓存中删除选择器
function deleteSelector(selectorId){
	var selector = getSelector();
	for(var i=0;i<selector.length;i++){
		if(selector[i] == selectorId){
			selector.splice(i,1);
			setSelector(selector);
			break;
		}
	}
}

//获取“更多”要展示的选择器，则是可扩展的选择器
function getMoreSelector(){
	return extendSelector;
}

//获取所有的选择器，包括默认选择器和可扩展选择器
function getAllSelector(){
	var selector = {};
	for(var i=0;i<defaultSelector.length;i++){
		selector[defaultSelector[i].id] = defaultSelector[i];
	}
	for(var i=0;i<extendSelector.length;i++){
		selector[extendSelector[i].id] = extendSelector[i];
	}
	return selector;
}

//通过选择器Id获取选择器
function getSelectorById(selectorId){
	var selectors = getAllSelector();
	if(selectors.hasOwnProperty(selectorId)){
		return selectors[selectorId];
	}
	return null;
}

function resetSelector(){
	var oldData = getSelectorValue();
	for(o in oldData) {
		oldData[o] = "";
		renderNewSelector(o, oldData[o], null);
	}
}

Handlebars.registerHelper("equals",function(v1,v2,options){
	 if(v1==v2){
	 	//满足添加继续执行
		return options.fn(this);
	 }else{
	 	//不满足条件执行{{else}}部分
	 	return options.inverse(this);
	 }
});

//点击事件的文本框显示下拉
$(document).on("click",".l-text-date>input",function(){
	  $(this).ligerDateEditor().bulidContent();
	  $(this).ligerDateEditor().toggleDateEditor($(this).ligerDateEditor().dateeditor.is(":visible"));
});

function showDate(date){ 
	var d = new Date(date);
	var m = d.getMonth()+1; 
	m = m<10 ? '0'+m : m;
	return d.getFullYear()+'-'+m+'-'+d.getDate(); 
} 

function addDate(date,days){ 
	var d=new Date(date);
	d.setDate(d.getDate()-days); 
	return showDate(d); 
}

function minuMonth(date,opt){ 
	var d = new Date(date);
	d.setMonth(d.getMonth() - parseInt(opt));
	return showDate(d);
}

function addWeek(date) {
	var now = new Date();
	var dates = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
	var year = dates.getFullYear();
	var month = dates.getMonth() + 1;
	month = month<10 ? '0'+month : month;
	var day = dates.getDate();
	return year + '-' + month + '-' + day;
}

var curDate = new Date();
function getYear(selectYear){
	var curYear = curDate.getFullYear();
	var year = [];
	for (var i = 0; i <= 10; i++) {
		var y = (curYear - i);
		if(selectYear!=null&&selectYear!=""){
			if(selectYear==y){
				year.push({id:y,text:y+"年",selected:1});
			}else{
				year.push({id:y,text:y+"年",selected:0});
			}
		}else{
			if(i==0){
				year.push({id:y,text:y+"年",selected:1});
			}else{
				year.push({id:y,text:y+"年",selected:0});
			}
		}
	}
	return year;
}

function getMonth(selectMonth){
	var month = []
	var curMonth = curDate.getMonth()+1;
	for(var i=1;i<13;i++){
		if(selectMonth!=null&&selectMonth!=""){
			if(selectMonth==i){
				month.push({id:i,text:i+"月",selected:1});
			}else{
				month.push({id:i,text:i+"月",selected:0});
			}
		}else{
			if(curMonth==i){
				month.push({id:i,text:i+"月",selected:1});
			}else{
				month.push({id:i,text:i+"月",selected:0});
			}
		}
	}
	return month;
}

function getCurYear(){
	return curDate.getFullYear();
}

function getCurMonth(){
	return curDate.getMonth()+1;
}
/*
 * 设置查询的条件到页面上
 */
function setQueryParamsToPage(content) {
	var condition = eval('(' + content + ')');
	if (moduleCode == 'goods' && condition.params) {
		$('.search-auto-entry').val(condition.params);
	} else {
		$('.search-auto-entry').val('');
	}
	for(c in condition) {
		renderNewSelector(c, condition[c], null);
	}
}
</script>